package com.szrt.cookie;

import java.io.IOException;
import java.net.CookieStore;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      

@WebServlet("/CookieController.action")
public class CookieController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) 
			
			throws ServletException, IOException {
		
		System.out.println("进入方法service....");
		
		String passLogin = request.getParameter("passLogin");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		
		Cookie[] cookies  = request.getCookies();
		
		System.out.println(cookies);
		
		System.out.println(cookies==null?null:cookies.length);
		
		if(cookies!=null && cookies.length>1){
			username=(String)request.getAttribute("username");
			password=(String)request.getAttribute("password");
		}
		
		
		if(null !=passLogin  && passLogin.equals("on")){
			
			
			
			
			
			//1 把用户名cookie 写到客户端的
			Cookie  usernameCookie = new Cookie("username", username);
			
			Cookie  passwordCookie = new Cookie("password", password);
			
			
			//设置失效
			usernameCookie.setMaxAge(60);
			passwordCookie.setMaxAge(60);
			
			
			//写入到客户端的c
			response.addCookie(usernameCookie);
			response.addCookie(passwordCookie);
			
			
		}
		
		
		//路径
		
		if("scott".equals(username)  && "123456".equals(password)){
			
			//登陆成功
			request.getRequestDispatcher("success.jsp").forward(request, response);
			
		}else{
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
		
		
		
	}

}
